package com.renfei.multimediatask.ui.task3;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.base.BaseActivity;
import com.renfei.multimediatask.ui.task2.RecordPcmDataActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

public class CamerPreviewActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    @BindView(R.id.btn_preview_surfaceview)
    Button btnPreviewSurfaceview;
    @BindView(R.id.btn_preview_textureView)
    Button btnPreviewTextureView;
    @BindView(R.id.btn_preview_Camera2)
    Button btnPreviewCamera2;


    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.CAMERA
    };
    private static final int PERMISSIONS_RECORD_CAMERA = 1001;


    private int eventFlag = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_camer_preview;
    }


    @Override
    protected void initListener() {

    }


    @OnClick({R.id.btn_preview_surfaceview, R.id.btn_preview_textureView, R.id.btn_preview_Camera2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_preview_surfaceview:
                eventFlag = 0;
                break;
            case R.id.btn_preview_textureView:
                eventFlag = 1;
                break;
            case R.id.btn_preview_Camera2:
                eventFlag = 2;
                break;
        }
        checkPreviewPermission();
    }


    private void checkPreviewPermission() {
        if (EasyPermissions.hasPermissions(this, PERMISSIONS)) {
            startPreviewPage();
        } else {
            applyPermission();
        }
    }

    private void applyPermission() {
        EasyPermissions.requestPermissions(this, "申请摄像头权限", PERMISSIONS_RECORD_CAMERA, PERMISSIONS);
    }

    private void startPreviewPage() {
        switch (eventFlag) {
            case 0:
                startActivity(PreviewSurfaceActivity.class);
                break;
            case 1:
                startActivity(PreviewTextureViewActivity.class);
                break;
            case 2:
                startActivity(PreviewCamera2Activity.class);
                break;
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_RECORD_CAMERA) {
            startPreviewPage();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_RECORD_CAMERA) {
            showToast("需要获取摄像头权限,否则无法使用");
        }
    }

}
